#ifndef TIMING_H
#define TIMING_H
void timer_callback( void );
void rtc_settime( unsigned int timedata );
void rtc_gettime( unsigned int *timedata );


#define A_Second                                 1000                    // 1s
#define A_Minute                                (60  * 1000)             // 1min
#define A_Hour                                  (60  * A_Minute)             // 1min

#define RS485_Transmit_Timeout                   50
#define Upload_To_Server_Timer_Max              (0.5 * A_Second)
#define System_Work_Timer_Max                   (24  * A_Hour)
#define Devie_Login_Timer_Max                   (5   * A_Second)
#define Devie_Login_Timer_Interval              (A_Second)
#define System_Sync_Clock_Timer_Max             (3   * A_Second)
#define System_Sync_Clock_Timer_Interval        (0.3 * A_Second)

#define Screen_Refresh_Timer_Max                ((0.3 * A_Second))
#define Screen_Stop_Push_Max                    ( 10 * A_Minute )
#define LED_Flicker_Cycle                        A_Second
#define Get_Sensor_Value_Timer_Max              (0.1 * A_Second)
#define Control_Motor_Timer_Max                 (0.1 * A_Minute)
#define Get_Single_Cycle                        (10 * A_Minute)


#define EC20_Init_Max                           (25 * A_Second)
#define EC20_Trigger_Timer_Max                  (0.8 * A_Second)
#define EC20_Connect_Timer_Interval             (0.5 * A_Second)

#define Scan_Alarm_Interval                     (3 * A_Second)
#define Scan_Malfunction_Interval               (3 * A_Second)
#define Heart_Data_Upload_Cycle                 (A_Minute)
//#define Heart_Data_Upload_Cycle                 (10 * A_Second)
#define Func_Data_Upload_Cycle                  (5 * A_Minute)          //业务数据上传周期
#define THA_Data_Upload_Cycle                   (30 * A_Second)         //工况数据上传周期

#define EC20_Stop_Work_Timer_Max                (2 * A_Minute)         //EC20 工作异常时间最大值 超过最大值EC20重启
#define EC20_Shutdown_Timer_Max                 (5 * A_Second)         //EC20 关机时间
#define Get_Ec20_Signal_Strength_Timer          (22 * A_Second)
#define Get_Ec20_Signal_Strength_Timer_Max      (3 * A_Second)
#define Get_Ec20_Signal_Strength_Timer_Step     (1.2 * A_Second)

#define Malfunction_Timer_Max                   (5 * A_Second)          //故障时间

#define Gate_Control_Timer_Max                  (0.1 * A_Second)

#define Alarm_Upload_Timer                      10
#define Malfunction_Upload_Timer                10

#define Sensor_Never_Data_Timer_Max             ( 1 * A_Minute )
typedef struct
{
  uint32_t device_login_timer;
  uint32_t system_sync_clock_timer;
  uint32_t led_work_timer;
  uint32_t system_work_timer;
  uint32_t get_XYflowmeter_value_timer;
  uint32_t get_ATMTencoder_value_timer;
  uint32_t get_sensor_value_timer;
  uint32_t set_motor_dutyfactor_timer;
  uint32_t upload_to_server_timer;
  uint32_t upload_to_server_heart_timer;
  uint32_t scan_alarm_timer;
  uint32_t scan_malfunction_timer;
  uint32_t gata_move_stop_timer;
  uint32_t gata_motionless_timer;
  uint32_t function_data_upload_timer;
  uint32_t THA_data_upload_timer;
  uint32_t water_level_upstream_stop_work_timer;
  uint32_t water_level_downstream_stop_work_timer;
  uint32_t flowmeter_stop_work_timer;
  uint32_t gate_control_timer;

  uint32_t EC20_connect_timer;
  uint32_t EC20_init_timer;
  uint32_t EC20_signal_timer;
  uint32_t EC20_get_signal_timer;
  uint32_t EC20_stop_work_timer;
  uint32_t EC20_trigger_timer;
  uint32_t EC20_shutdown_timer;

  uint32_t encoder_recv_stop_timer;
  uint32_t wait_confirm_timeout_timer;
  uint32_t screen_refresh_timer;
  uint32_t RS485_transmit_timer;
  uint32_t screen_stop_push_timer;

  uint32_t proc_get_single_timer;

  uint32_t sensor_water_level_timer;
  uint32_t sensor_flowmeter_timer;
}__attribute__((packed, aligned(1))) TIMER;
void set_device_clock( uint8_t *data  );
void check_clock_sync( uint8_t *data );
#endif
